package com.bit101.components{
	import com.bit101.components.HScrollBar;
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.display.DisplayObjectContainer;
	import com.bit101.components.Component;
	import flash.display.Sprite;

	/**
 * @author ase7en
 */
	public class HScrollBarBox extends Component {
		private var _child:DisplayObject;
		private var _scrollbar : HScrollBar;
		private var maskMc : Sprite;
		private var _ractW : Number;
		private var _ractH : Number;
		private var _lineSize:Number;
		public function HScrollBarBox(child:DisplayObject,ractW:Number,ractH:Number,lineSize:Number = 1,parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0) {
			
			_child = child;
			_ractW = ractW;
			_ractH = ractH;
			_lineSize = lineSize;
			this.addChild(_child);
			super(parent, xpos, ypos);
		}
		
		/**
		 * Creates and adds the child display objects of this component.
		 */
		override protected function addChildren():void
		{
			super.addChildren();
			_scrollbar = new HScrollBar(this, 0, 0, onScrollbarScroll);
			maskMc = new Sprite();
			maskMc.graphics.beginFill(0x000000, 1);
			maskMc.graphics.drawRect(0, 0, _ractW, _ractH);
			maskMc.graphics.endFill();
			_child.mask = maskMc;
			_scrollbar.setThumbPercent(_ractW / _child.width);
			_scrollbar.setSliderParams(0, _child.width - _ractW, 0);
			_scrollbar.y = _ractH;
			_scrollbar.width = _ractW;
			_scrollbar.lineSize = _lineSize;
		}
		
		/**
		 * Called when the scroll bar is moved. Scrolls text accordingly.
		 */
		protected function onScrollbarScroll(event:Event):void {
			_child.x = - Math.round(_scrollbar.value);
		}
		
		public function get scrollbar():HScrollBar { return _scrollbar; }
	}
}
